/**
 * Created by mrcode on 2016/2/27.
 * 用户内页公用js
 */

$(function () {
    sendRecordUser();
});



//换背景功能
var msgTemplet =
    '<div class="alert {0} alert-dismissible text-center" role="alert">' +
    '<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>' +
    '<span>{1}</span>' +
    '</div>';

var $hu = $("#hu-bg");
var handerBg = $hu.attr('handerBg');
$("#hbj-modal img[handerBg='" + handerBg + "']").parent(".view-warp").addClass("active");

var hu_bg_url = $hu.css('background-image');
$("#hbj-btn").on('click', function () { //打开窗口
    MrcodeUi.Modal_dialog.center('#hbj-modal');
    $('#hbj-modal').modal({backdrop: false});
    $("body").css({"padding-right": 0});
});
$("#hbj-modal img").on('click', function () { //设置保存
    hu_bg_url = "url(" + $(this).attr('src') + ")";
    $("#hu-bg").css('background-image', hu_bg_url);
    $("#hbj-modal .view-warp").removeClass("active");
    $(this).parent(".view-warp").addClass("active");
    var handerBg_up = $(this).attr('handerBg');
    if (handerBg == handerBg_up) { //已经保存过的不再保存
        return;
    }
    $.ajax({
        url: $contextPath + "/auth/user/sethubg",
        data: {
            handerBg: handerBg_up
        },
        type: "POST",
        dataType: 'json',
        success: function (data) {
            var success = data.success;
            var message = data.message;
            var url = data.url;
            if (1 == success) {
             /*   $("#hbj-notice-warp").empty();
                $("#hbj-notice-warp").append(handerTemplet(msgTemplet, ['alert-success', message]));*/
                MrcodeUi.Alert.sx.success(message);
                handerBg = handerBg_up;
            } else {
                if (url) {
                    window.location.href = url;
                }
              /*  $("#hbj-notice-warp").empty();
                $("#hbj-notice-warp").append(handerTemplet(msgTemplet, ['alert-danger', message]));*/
                MrcodeUi.Alert.sx.danger(message);
            }
        },
        error: function (er) {
            var mrcodePopLayer = new MrcodePopLayer({
                'position':"center",
                'animateEntrance':'flipInX',
                'animateClosing':'flipOutX',
                'content':"请先登录",
                'dialog':"confirm",
                'onCancel':{text: '我还不想登录'},
                'onConfirm':{
                    text: '去登录',
                    action:function(){
                        window.location.href = "/account/login";
                        return true;
                    }
                }
            });
            
        }
    });
});

$("#hbj-modal .view-warp").hover(function () { //预览
    $("#hu-bg").css('background-image', "url(" + $(this).find('img').attr('src') + ")");
    $("#hbj-notice-warp").empty();
}, function () {
    $("#hu-bg").css('background-image', hu_bg_url);
});


/** ===================================== */

function sendRecordUser(){
    $.post($contextPath + "/stat/users", {
        type:2,
        id:$("#hi_bcid").val()
    });
/*    var ck = getCookie("recordUser");
    if(!ck){
        $.post($contextPath + "/tj/article", {
            type:1,
            id:$("#articleId").val()
        });
    }*/
}